package com.ys.util;

import java.math.BigDecimal;

public class DoubleUtil {

    public static double round(double value, int scale) {
        int n = (int)Math.pow(10.0D, (double)scale);
        return divide(Math.floor(multiply(value, (double)n)), (double)n, scale);
    }

    public static double roundUp(double value, int scale) {
        int n = (int)Math.pow(10.0D, (double)scale);
        double result = divide(Math.ceil(multiply(value, (double)n)), (double)n, scale);
        return result;
    }

    public static double add(double d1, double... d2s) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        double[] var4 = d2s;
        int var5 = d2s.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            double d2 = var4[var6];
            BigDecimal bd2 = new BigDecimal(Double.toString(d2));
            bd1 = bd1.add(bd2);
        }

        return bd1.doubleValue();
    }

    public static double subtract(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }

    public static double multiply(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.multiply(bd2).doubleValue();
    }

    public static double multiply(double d1, double... d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        double[] var4 = d2;
        int var5 = d2.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            double d = var4[var6];
            bd1 = bd1.multiply(new BigDecimal(Double.toString(d)));
        }

        return bd1.doubleValue();
    }

    public static double divide(double d1, double d2, int scale) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.divide(bd2, scale, 4).doubleValue();
    }
}
